package com.zhs.gccs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zhs.gccs.BuildConfig;
import com.zhs.gccs.R;
import com.zhs.gccs.model.CaFile;
import com.zhs.gccs.utils.SharedPrefHelper;
import com.zhs.gccs.view.ShapeImageView;

import java.util.List;

/**
 * Created by 任伟伟
 * Datetime: 2016/11/18-11:07
 * Email: renweiwei@ufashion.com
 */

public class GccsTakeImageAdapter extends RecyclerView.Adapter<GccsTakeImageAdapter.ViewHolder> {
    private Context mContext;
    private List<CaFile> datas = null;

    public GccsTakeImageAdapter(Context context, List<CaFile> datas) {
        this.datas = datas;
        this.mContext = context;
    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_view_take_image_item,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
        CaFile image = datas.get(position);
        if(image.isPlaceholder()){
            //最后一个是添加
            viewHolder.v_x.setVisibility(View.GONE);
            viewHolder.v_img.setVisibility(View.GONE);
            viewHolder.v_stand.setVisibility(View.VISIBLE);
            if(CaFile.type_image.equals(image.getImgType())){
                Glide.with(mContext).load(R.mipmap.photograph).into(viewHolder.v_stand);
            }else if(CaFile.type_video.equals(image.getImgType())){
                Glide.with(mContext).load(R.mipmap.video).into(viewHolder.v_stand);
            }else if(CaFile.type_audio.equals(image.getImgType())){
                Glide.with(mContext).load(R.mipmap.audio).into(viewHolder.v_stand);
            }
            viewHolder.item_img_list.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(operationListener!=null)operationListener.selectPhoto(image);
                }
            });
        }
        else {
            viewHolder.v_x.setVisibility(image.isEditable()?View.VISIBLE:View.GONE);
            if(CaFile.type_audio.equals(image.getImgType())){
                viewHolder.v_img.setVisibility(View.GONE);
                viewHolder.v_stand.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(R.mipmap.audio).into(viewHolder.v_stand);
            }else if(CaFile.type_image.equals(image.getImgType())){
                if(!TextUtils.isEmpty(image.getLocalPath())){
                    Glide.with(mContext).load(image.getLocalPath()).into(viewHolder.v_img);
                }else if(!TextUtils.isEmpty(image.getFileId())){
                    Glide.with(mContext).
                            load(BuildConfig.BASEFILEURL+image.getFileId()
                                    +"?token="+ SharedPrefHelper.getInstance().getToken())
                            .into(viewHolder.v_img);
                }
                viewHolder.v_img.setVisibility(View.VISIBLE);
                viewHolder.v_stand.setVisibility(View.GONE);
            }else if(CaFile.type_video.equals(image.getImgType())){
                if(!TextUtils.isEmpty(image.getVideoImgUrl())){
                    Glide.with(mContext).load(image.getVideoImgUrl()).into(viewHolder.v_img);
                }else if(!TextUtils.isEmpty(image.getFileId())){
                    //
                    Glide.with(mContext).
                            load(BuildConfig.BASEFILEPREVIEWURL+image.getFileId()
                                    +"?token="+ SharedPrefHelper.getInstance().getToken())
                            .into(viewHolder.v_img);
                }
                viewHolder.v_img.setVisibility(View.VISIBLE);
                viewHolder.v_stand.setVisibility(View.GONE);
            }
            viewHolder.item_img_list.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(operationListener!=null)operationListener.onItemPreview(position,image);
                }
            });
            viewHolder.v_x.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(operationListener!=null)operationListener.onItemDelete(position,image);
                }
            });
        }
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
//        if(datas!=null&&datas.size()>3)return 3;
        return datas!=null?datas.size():0;
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View item_img_list;
        public ShapeImageView v_img;
        public View v_x;//删除
        public ImageView v_stand;
        public TextView tv_angle;
        public TextView tv_degree;
        public ViewHolder(View view){
            super(view);
            item_img_list =   view.findViewById(R.id.item_img_list);
            v_img =   view.findViewById(R.id.v_img);
            v_x =   view.findViewById(R.id.v_x);
            v_stand =   view.findViewById(R.id.v_stand);
            tv_angle =   view.findViewById(R.id.tv_angle);
            tv_degree =   view.findViewById(R.id.tv_degree);
        }
    }
    public static interface OperationListener{
        void onItemPreview(int position, CaFile image);
        void selectPhoto(CaFile image);
        void onItemDelete(int position, CaFile image);
    }
    private OperationListener operationListener;

    public OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
